package com.rocket.database;

import java.util.ArrayList;

import android.content.ContentValues;
import android.database.Cursor;

import com.rocket.entity.Missile83;

public class Missile83Sql {

	private static Missile83Sql	instance				= null;
	private final String		TABLE_NAME				= "Missile83";

	private final String		KEY_MISSILE83_ID		= "id";
	private final String		KEY_MISSILE83_ANGLE		= "angle";		//角度
	private final String		KEY_MISSILE83_TIME		= "time";
	private final String		KEY_MISSILE83_HEIGHT	= "height";
	private final String		KEY_MISSILE83_LENGTH	= "length";
	private final String		KEY_MISSILE83_TOPHEIGHT	= "topheight";
	private final String		KEY_MISSILE83_TOPLENGTH	= "toplength";
	private final String		KEY_MISSILE83_RANGE		= "range";

	public static synchronized Missile83Sql getInstance() {
		if (null == instance) {
			instance = new Missile83Sql();
		}
		return instance;
	}

	public void init() {
		createTable();
		initData();
	}

	private void createTable() {
		if (DataBaseOpenHelper.mDataBase != null) {
			String sql = "drop table if exists " + TABLE_NAME;
			DataBaseOpenHelper.mDataBase.execSQL(sql);
			sql = "create table if not exists " + TABLE_NAME + "( " + KEY_MISSILE83_ID + " INTEGER PRIMARY KEY," + KEY_MISSILE83_ANGLE + " INTEGER," + KEY_MISSILE83_TIME + " INTEGER," + KEY_MISSILE83_HEIGHT + " INTEGER," + KEY_MISSILE83_LENGTH + " INTEGER,"
					+ KEY_MISSILE83_TOPHEIGHT + " INTEGER," + KEY_MISSILE83_TOPLENGTH + " INTEGER," + KEY_MISSILE83_RANGE + " INTEGER)";
			DataBaseOpenHelper.mDataBase.execSQL(sql);
		}
	}

	public long insert(Missile83 missile) {
		if (DataBaseOpenHelper.mDataBase != null) {
			ContentValues values = new ContentValues();
			values.put(KEY_MISSILE83_ANGLE, missile.angel);
			values.put(KEY_MISSILE83_TIME, missile.time);
			values.put(KEY_MISSILE83_HEIGHT, missile.height);
			values.put(KEY_MISSILE83_LENGTH, missile.length);
			values.put(KEY_MISSILE83_TOPHEIGHT, missile.topHeight);
			values.put(KEY_MISSILE83_TOPLENGTH, missile.topLength);
			values.put(KEY_MISSILE83_RANGE, missile.range);
			return DataBaseOpenHelper.mDataBase.insert(TABLE_NAME, null, values);
		}
		return 0;
	}

	public long update(Missile83 missile) {
		if (DataBaseOpenHelper.mDataBase != null) {
			ContentValues values = new ContentValues();
			values.put(KEY_MISSILE83_ANGLE, missile.angel);
			values.put(KEY_MISSILE83_TIME, missile.time);
			values.put(KEY_MISSILE83_HEIGHT, missile.height);
			values.put(KEY_MISSILE83_LENGTH, missile.length);
			values.put(KEY_MISSILE83_TOPHEIGHT, missile.topHeight);
			values.put(KEY_MISSILE83_TOPLENGTH, missile.topLength);
			values.put(KEY_MISSILE83_RANGE, missile.range);
			return DataBaseOpenHelper.mDataBase.update(TABLE_NAME, values, KEY_MISSILE83_ID + " = ?", new String[] { String.valueOf(missile.id) });
		}
		return 0;
	}

	public void delete(Missile83 missile) {
		delete(missile.id);
	}

	public void delete(int id) {
		if (DataBaseOpenHelper.mDataBase != null) {
			DataBaseOpenHelper.mDataBase.delete(TABLE_NAME, KEY_MISSILE83_ID + " = ?", new String[] { String.valueOf(id) });
		}
	}

	public ArrayList<Missile83> query() {
		ArrayList<Missile83> missiles = new ArrayList<Missile83>();
		if (DataBaseOpenHelper.mDataBase != null) {
			Cursor cur = null;
			try {
				DataBaseOpenHelper.mDataBase.beginTransaction();
				cur = DataBaseOpenHelper.mDataBase.query(TABLE_NAME, null, null, null, null, null, KEY_MISSILE83_HEIGHT);
				if (cur != null && cur.moveToFirst()) {
					do {
						Missile83 missile = new Missile83();
						missile.id = cur.getInt(cur.getColumnIndex(KEY_MISSILE83_ID));
						missile.angel = cur.getInt(cur.getColumnIndex(KEY_MISSILE83_ANGLE));
						missile.time = cur.getInt(cur.getColumnIndex(KEY_MISSILE83_TIME));
						missile.height = cur.getInt(cur.getColumnIndex(KEY_MISSILE83_HEIGHT));
						missile.length = cur.getInt(cur.getColumnIndex(KEY_MISSILE83_LENGTH));
						missile.topHeight = cur.getInt(cur.getColumnIndex(KEY_MISSILE83_TOPHEIGHT));
						missile.topLength = cur.getInt(cur.getColumnIndex(KEY_MISSILE83_TOPLENGTH));
						missile.range = cur.getInt(cur.getColumnIndex(KEY_MISSILE83_RANGE));
						missiles.add(missile);
					} while (cur.moveToNext());
				}
				DataBaseOpenHelper.mDataBase.setTransactionSuccessful();
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (cur != null) {
					cur.close();
					cur = null;
				}
				DataBaseOpenHelper.mDataBase.endTransaction();
			}
		}
		return missiles;
	}

	public ArrayList<Missile83> queryWithAngle(int angle) {
		ArrayList<Missile83> missiles = new ArrayList<Missile83>();
		if (DataBaseOpenHelper.mDataBase != null) {
			Cursor cur = null;
			try {
				DataBaseOpenHelper.mDataBase.beginTransaction();
				cur = DataBaseOpenHelper.mDataBase.query(TABLE_NAME, null, KEY_MISSILE83_ANGLE + " = ? ", new String[] { String.valueOf(angle) }, null, null, KEY_MISSILE83_TIME);
				if (cur != null && cur.moveToFirst()) {
					do {
						Missile83 missile = new Missile83();
						missile.id = cur.getInt(cur.getColumnIndex(KEY_MISSILE83_ID));
						missile.angel = cur.getInt(cur.getColumnIndex(KEY_MISSILE83_ANGLE));
						missile.time = cur.getInt(cur.getColumnIndex(KEY_MISSILE83_TIME));
						missile.height = cur.getInt(cur.getColumnIndex(KEY_MISSILE83_HEIGHT));
						missile.length = cur.getInt(cur.getColumnIndex(KEY_MISSILE83_LENGTH));
						missile.topHeight = cur.getInt(cur.getColumnIndex(KEY_MISSILE83_TOPHEIGHT));
						missile.topLength = cur.getInt(cur.getColumnIndex(KEY_MISSILE83_TOPLENGTH));
						missile.range = cur.getInt(cur.getColumnIndex(KEY_MISSILE83_RANGE));
						missiles.add(missile);
					} while (cur.moveToNext());
				}
				DataBaseOpenHelper.mDataBase.setTransactionSuccessful();
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (cur != null) {
					cur.close();
					cur = null;
				}
				DataBaseOpenHelper.mDataBase.endTransaction();
			}
		}
		return missiles;
	}

	public ArrayList<Missile83> query(int time) {
		ArrayList<Missile83> missiles = new ArrayList<Missile83>();
		if (DataBaseOpenHelper.mDataBase != null) {
			Cursor cur = null;
			try {
				DataBaseOpenHelper.mDataBase.beginTransaction();
				cur = DataBaseOpenHelper.mDataBase.query(TABLE_NAME, null, KEY_MISSILE83_TIME + " = ? ", new String[] { String.valueOf(time) }, null, null, KEY_MISSILE83_HEIGHT);
				if (cur != null && cur.moveToFirst()) {
					do {
						Missile83 missile = new Missile83();
						missile.id = cur.getInt(cur.getColumnIndex(KEY_MISSILE83_ID));
						missile.angel = cur.getInt(cur.getColumnIndex(KEY_MISSILE83_ANGLE));
						missile.time = cur.getInt(cur.getColumnIndex(KEY_MISSILE83_TIME));
						missile.height = cur.getInt(cur.getColumnIndex(KEY_MISSILE83_HEIGHT));
						missile.length = cur.getInt(cur.getColumnIndex(KEY_MISSILE83_LENGTH));
						missile.topHeight = cur.getInt(cur.getColumnIndex(KEY_MISSILE83_TOPHEIGHT));
						missile.topLength = cur.getInt(cur.getColumnIndex(KEY_MISSILE83_TOPLENGTH));
						missile.range = cur.getInt(cur.getColumnIndex(KEY_MISSILE83_RANGE));
						missiles.add(missile);
					} while (cur.moveToNext());
				}
				DataBaseOpenHelper.mDataBase.setTransactionSuccessful();
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (cur != null) {
					cur.close();
					cur = null;
				}
				DataBaseOpenHelper.mDataBase.endTransaction();
			}
		}
		return missiles;
	}

	private void initData() {
		Missile83 missile = new Missile83();

		missile.angel = 45;
		missile.time = 8;
		missile.height = 2557;
		missile.length = 2790;
		missile.topHeight = 4250;
		missile.topLength = 6750;
		missile.range = 10800;
		insert(missile);

		missile.angel = 45;
		missile.time = 10;
		missile.height = 2877;
		missile.length = 3239;
		missile.topHeight = 4250;
		missile.topLength = 6750;
		missile.range = 10800;
		insert(missile);

		missile.angel = 45;
		missile.time = 12;
		missile.height = 3142;
		missile.length = 3663;
		missile.topHeight = 4250;
		missile.topLength = 6750;
		missile.range = 10800;
		insert(missile);

		missile.angel = 45;
		missile.time = 14;
		missile.height = 3355;
		missile.length = 4067;
		missile.topHeight = 4250;
		missile.topLength = 6750;
		missile.range = 10800;
		insert(missile);

		missile.angel = 45;
		missile.time = 16;
		missile.height = 3520;
		missile.length = 4453;
		missile.topHeight = 4250;
		missile.topLength = 6750;
		missile.range = 10800;
		insert(missile);

		missile.angel = 45;
		missile.time = 18;
		missile.height = 3641;
		missile.length = 4824;
		missile.topHeight = 4250;
		missile.topLength = 6750;
		missile.range = 10800;
		insert(missile);

		missile.angel = 45;
		missile.time = 20;
		missile.height = 3719;
		missile.length = 5182;
		missile.topHeight = 4250;
		missile.topLength = 6750;
		missile.range = 10800;
		insert(missile);

		missile.angel = 50;
		missile.time = 8;
		missile.height = 2801;
		missile.length = 2546;
		missile.topHeight = 4600;
		missile.topLength = 6100;
		missile.range = 10000;
		insert(missile);

		missile.angel = 50;
		missile.time = 10;
		missile.height = 3163;
		missile.length = 2959;
		missile.topHeight = 4600;
		missile.topLength = 6100;
		missile.range = 10000;
		insert(missile);

		missile.angel = 50;
		missile.time = 12;
		missile.height = 3468;
		missile.length = 3394;
		missile.topHeight = 4600;
		missile.topLength = 6100;
		missile.range = 10000;
		insert(missile);

		missile.angel = 50;
		missile.time = 14;
		missile.height = 3720;
		missile.length = 3722;
		missile.topHeight = 4600;
		missile.topLength = 6100;
		missile.range = 10000;
		insert(missile);

		missile.angel = 50;
		missile.time = 16;
		missile.height = 3923;
		missile.length = 4079;
		missile.topHeight = 4600;
		missile.topLength = 6100;
		missile.range = 10000;
		insert(missile);

		missile.angel = 50;
		missile.time = 18;
		missile.height = 4081;
		missile.length = 4423;
		missile.topHeight = 4600;
		missile.topLength = 6100;
		missile.range = 10000;
		insert(missile);

		missile.angel = 50;
		missile.time = 20;
		missile.height = 4194;
		missile.length = 4756;
		missile.topHeight = 4600;
		missile.topLength = 6100;
		missile.range = 10000;
		insert(missile);

		missile.angel = 55;
		missile.time = 8;
		missile.height = 3022;
		missile.length = 2280;
		missile.topHeight = 4950;
		missile.topLength = 5550;
		missile.range = 9150;
		insert(missile);

		missile.angel = 55;
		missile.time = 10;
		missile.height = 3423;
		missile.length = 2652;
		missile.topHeight = 4950;
		missile.topLength = 5550;
		missile.range = 9150;
		insert(missile);

		missile.angel = 55;
		missile.time = 12;
		missile.height = 3765;
		missile.length = 3004;
		missile.topHeight = 4950;
		missile.topLength = 5550;
		missile.range = 9150;
		insert(missile);

		missile.angel = 55;
		missile.time = 14;
		missile.height = 4053;
		missile.length = 3341;
		missile.topHeight = 4950;
		missile.topLength = 5550;
		missile.range = 9150;
		insert(missile);

		missile.angel = 55;
		missile.time = 16;
		missile.height = 4292;
		missile.length = 3665;
		missile.topHeight = 4950;
		missile.topLength = 5550;
		missile.range = 9150;
		insert(missile);

		missile.angel = 55;
		missile.time = 18;
		missile.height = 4484;
		missile.length = 3977;
		missile.topHeight = 4950;
		missile.topLength = 5550;
		missile.range = 9150;
		insert(missile);

		missile.angel = 55;
		missile.time = 20;
		missile.height = 4631;
		missile.length = 4208;
		missile.topHeight = 4950;
		missile.topLength = 5550;
		missile.range = 9150;
		insert(missile);

		missile.angel = 60;
		missile.time = 8;
		missile.height = 3219;
		missile.length = 1994;
		missile.topHeight = 5350;
		missile.topLength = 4900;
		missile.range = 8250;
		insert(missile);

		missile.angel = 60;
		missile.time = 10;
		missile.height = 3654;
		missile.length = 2321;
		missile.topHeight = 5350;
		missile.topLength = 4900;
		missile.range = 8250;
		insert(missile);

		missile.angel = 60;
		missile.time = 12;
		missile.height = 4030;
		missile.length = 2631;
		missile.topHeight = 5350;
		missile.topLength = 4900;
		missile.range = 8250;
		insert(missile);

		missile.angel = 60;
		missile.time = 14;
		missile.height = 4351;
		missile.length = 2928;
		missile.topHeight = 5350;
		missile.topLength = 4900;
		missile.range = 8250;
		insert(missile);

		missile.angel = 60;
		missile.time = 16;
		missile.height = 4621;
		missile.length = 3214;
		missile.topHeight = 5350;
		missile.topLength = 4900;
		missile.range = 8250;
		insert(missile);

		missile.angel = 60;
		missile.time = 18;
		missile.height = 4844;
		missile.length = 3491;
		missile.topHeight = 5350;
		missile.topLength = 4900;
		missile.range = 8250;
		insert(missile);

		missile.angel = 60;
		missile.time = 20;
		missile.height = 5022;
		missile.length = 3760;
		missile.topHeight = 5350;
		missile.topLength = 4900;
		missile.range = 8250;
		insert(missile);

		missile.angel = 65;
		missile.time = 8;
		missile.height = 3389;
		missile.length = 1690;
		missile.topHeight = 5800;
		missile.topLength = 4200;
		missile.range = 7300;
		insert(missile);

		missile.angel = 65;
		missile.time = 10;
		missile.height = 3864;
		missile.length = 1968;
		missile.topHeight = 5800;
		missile.topLength = 4200;
		missile.range = 7300;
		insert(missile);

		missile.angel = 65;
		missile.time = 12;
		missile.height = 4259;
		missile.length = 2232;
		missile.topHeight = 5800;
		missile.topLength = 4200;
		missile.range = 7300;
		insert(missile);

		missile.angel = 65;
		missile.time = 14;
		missile.height = 4609;
		missile.length = 2486;
		missile.topHeight = 5800;
		missile.topLength = 4200;
		missile.range = 7300;
		insert(missile);

		missile.angel = 65;
		missile.time = 16;
		missile.height = 4907;
		missile.length = 2731;
		missile.topHeight = 5800;
		missile.topLength = 4200;
		missile.range = 7300;
		insert(missile);

		missile.angel = 65;
		missile.time = 18;
		missile.height = 5158;
		missile.length = 2968;
		missile.topHeight = 5800;
		missile.topLength = 4200;
		missile.range = 7300;
		insert(missile);

		missile.angel = 65;
		missile.time = 20;
		missile.height = 5363;
		missile.length = 3199;
		missile.topHeight = 5800;
		missile.topLength = 4200;
		missile.range = 7300;
		insert(missile);

		missile.angel = 70;
		missile.time = 8;
		missile.height = 3530;
		missile.length = 1370;
		missile.topHeight = 6200;
		missile.topLength = 3500;
		missile.range = 6300;
		insert(missile);

		missile.angel = 70;
		missile.time = 10;
		missile.height = 4021;
		missile.length = 1597;
		missile.topHeight = 6200;
		missile.topLength = 3500;
		missile.range = 6300;
		insert(missile);

		missile.angel = 70;
		missile.time = 12;
		missile.height = 4451;
		missile.length = 1813;
		missile.topHeight = 6200;
		missile.topLength = 3500;
		missile.range = 6300;
		insert(missile);

		missile.angel = 70;
		missile.time = 14;
		missile.height = 4824;
		missile.length = 2020;
		missile.topHeight = 6200;
		missile.topLength = 3500;
		missile.range = 6300;
		insert(missile);

		missile.angel = 70;
		missile.time = 16;
		missile.height = 5147;
		missile.length = 2220;
		missile.topHeight = 6200;
		missile.topLength = 3500;
		missile.range = 6300;
		insert(missile);

		missile.angel = 70;
		missile.time = 18;
		missile.height = 5421;
		missile.length = 2414;
		missile.topHeight = 6200;
		missile.topLength = 3500;
		missile.range = 6300;
		insert(missile);

		missile.angel = 70;
		missile.time = 20;
		missile.height = 5649;
		missile.length = 2603;
		missile.topHeight = 6200;
		missile.topLength = 3500;
		missile.range = 6300;
		insert(missile);

		missile.angel = 75;
		missile.time = 8;
		missile.height = 3642;
		missile.length = 1039;
		missile.topHeight = 6600;
		missile.topLength = 2800;
		missile.range = 5200;
		insert(missile);

		missile.angel = 75;
		missile.time = 10;
		missile.height = 4153;
		missile.length = 1211;
		missile.topHeight = 6600;
		missile.topLength = 2800;
		missile.range = 5200;
		insert(missile);

		missile.angel = 75;
		missile.time = 12;
		missile.height = 4602;
		missile.length = 1375;
		missile.topHeight = 6600;
		missile.topLength = 2800;
		missile.range = 5200;
		insert(missile);

		missile.angel = 75;
		missile.time = 14;
		missile.height = 4995;
		missile.length = 1533;
		missile.topHeight = 6600;
		missile.topLength = 2800;
		missile.range = 5200;
		insert(missile);

		missile.angel = 75;
		missile.time = 16;
		missile.height = 5336;
		missile.length = 1685;
		missile.topHeight = 6600;
		missile.topLength = 2800;
		missile.range = 5200;
		insert(missile);

		missile.angel = 75;
		missile.time = 18;
		missile.height = 5629;
		missile.length = 1833;
		missile.topHeight = 6600;
		missile.topLength = 2800;
		missile.range = 5200;
		insert(missile);

		missile.angel = 75;
		missile.time = 20;
		missile.height = 5876;
		missile.length = 1978;
		missile.topHeight = 6600;
		missile.topLength = 2800;
		missile.range = 5200;
		insert(missile);

		missile.angel = 80;
		missile.time = 8;
		missile.height = 3722;
		missile.length = 698;
		missile.topHeight = 7100;
		missile.topLength = 2100;
		missile.range = 3950;
		insert(missile);

		missile.angel = 80;
		missile.time = 10;
		missile.height = 4248;
		missile.length = 930;
		missile.topHeight = 7100;
		missile.topLength = 2100;
		missile.range = 3950;
		insert(missile);

		missile.angel = 80;
		missile.time = 12;
		missile.height = 4711;
		missile.length = 814;
		missile.topHeight = 7100;
		missile.topLength = 2100;
		missile.range = 3950;
		insert(missile);

		missile.angel = 80;
		missile.time = 14;
		missile.height = 5118;
		missile.length = 1030;
		missile.topHeight = 7100;
		missile.topLength = 2100;
		missile.range = 3950;
		insert(missile);

		missile.angel = 80;
		missile.time = 16;
		missile.height = 5473;
		missile.length = 1133;
		missile.topHeight = 7100;
		missile.topLength = 2100;
		missile.range = 3950;
		insert(missile);

		missile.angel = 80;
		missile.time = 18;
		missile.height = 5780;
		missile.length = 1233;
		missile.topHeight = 7100;
		missile.topLength = 2100;
		missile.range = 3950;
		insert(missile);

		missile.angel = 80;
		missile.time = 20;
		missile.height = 6040;
		missile.length = 1331;
		missile.topHeight = 7100;
		missile.topLength = 2100;
		missile.range = 3950;
		insert(missile);

		missile.angel = 85;
		missile.time = 8;
		missile.height = 3771;
		missile.length = 375;
		missile.topHeight = 7750;
		missile.topLength = 1200;
		missile.range = 2100;
		insert(missile);

		missile.angel = 85;
		missile.time = 10;
		missile.height = 4306;
		missile.length = 409;
		missile.topHeight = 7750;
		missile.topLength = 1200;
		missile.range = 2100;
		insert(missile);

		missile.angel = 85;
		missile.time = 12;
		missile.height = 4778;
		missile.length = 464;
		missile.topHeight = 7750;
		missile.topLength = 1200;
		missile.range = 2100;
		insert(missile);

		missile.angel = 85;
		missile.time = 14;
		missile.height = 5193;
		missile.length = 518;
		missile.topHeight = 7750;
		missile.topLength = 1200;
		missile.range = 2100;
		insert(missile);

		missile.angel = 85;
		missile.time = 16;
		missile.height = 5556;
		missile.length = 569;
		missile.topHeight = 7750;
		missile.topLength = 1200;
		missile.range = 2100;
		insert(missile);

		missile.angel = 85;
		missile.time = 18;
		missile.height = 5871;
		missile.length = 620;
		missile.topHeight = 7750;
		missile.topLength = 1200;
		missile.range = 2100;
		insert(missile);

		missile.angel = 85;
		missile.time = 20;
		missile.height = 6140;
		missile.length = 669;
		missile.topHeight = 7750;
		missile.topLength = 1200;
		missile.range = 2100;
		insert(missile);
	}
}
